package top.glkj.hms.entity;

import lombok.Getter;
import lombok.Setter;
import org.hibernate.annotations.Fetch;
import org.hibernate.annotations.FetchMode;
import top.glkj.hms.utils.Status;

import javax.persistence.*;
import java.util.List;

/**
 * 房间表，记录酒店房间信息
 *
 * @author : xgl
 * @version : 0.0.1
 * @date :2020/11/1 0:36
 * 计价方法：基础费用加房租费用*时间
 */
@Getter
@Setter
@Table(name = "hms_room")
@Entity
public class Room extends BaseEntity {
    /**
     * 房间号
     */
    @Column(name = "room_code", nullable = false, unique = true)
    private String roomCode;
    /**
     * 日租费用
     */
    @Column(name = "daily_rent")
    private double dailyRent;
    /**
     * 时租费用
     */
    @Column(name = "hourly_rate")
    private double hourlyRate;
    /**
     * 多日租折扣
     */
    private double discount = 1;

    /**
     * 活动折扣
     */
    @Column(name = "activity_discount")
    private double activityDiscount = 1;

    /**
     * 活动优惠
     */
    @Column(name = "activity_offers")
    private double activityOffers = 0;
    /**
     * 房子类型
     */
    @OneToOne(targetEntity = RoomType.class, cascade = CascadeType.ALL, fetch = FetchType.EAGER)
    @JoinColumn(name = "room_type_id", referencedColumnName = "id")
    private RoomType roomType;
    @OneToOne(targetEntity = Img.class, cascade = CascadeType.ALL, fetch = FetchType.LAZY)
    @JoinColumn(name = "img_id", referencedColumnName = "id")
    private Img mainImg;
    /**
     *
     */
    @Fetch(FetchMode.SUBSELECT)
    @ManyToMany(targetEntity = Img.class, cascade = CascadeType.ALL, fetch = FetchType.LAZY)
    @JoinTable(name = "hms_room_imgs",
            //当前对象在中间表中的外键
            joinColumns = {@JoinColumn(name = "room_id", referencedColumnName = "id")},
            //对方对象在中间表中的外键
            inverseJoinColumns = {@JoinColumn(name = "img_id", referencedColumnName = "id")}
    )
    private List<Img> imgList;
    /**
     * 物损费
     */
    @Column(name = "property_damage")
    private double propertyDamage = 0;
    /**
     * 入住人数
     */
    @Column(name = "into_count")
    private int intoCount = 0;
    @Column(name = "views")
    private int views = 0;
    /**
     * 房屋状态
     */
    private Status status;

    /**
     * 入住用户ID
     */
    @OneToOne(targetEntity = User.class, cascade = CascadeType.ALL, fetch = FetchType.EAGER)
    @JoinColumn(name = "insert_user_id", referencedColumnName = "id")
    private User insertUser;

    /**
     * 入住用户ID
     */
    @Fetch(FetchMode.SUBSELECT)
    @ManyToMany(targetEntity = User.class, cascade = CascadeType.ALL, fetch = FetchType.LAZY)
    @JoinTable(name = "hms_room_into_users",
            //当前对象在中间表中的外键
            joinColumns = {@JoinColumn(name = "room_id", referencedColumnName = "id")},
            //对方对象在中间表中的外键
            inverseJoinColumns = {@JoinColumn(name = "user_id", referencedColumnName = "id")}
    )
    private List<User> intoUsers;

    /**
     * 该类型房间下的的特有基础设施
     */
    @Fetch(FetchMode.SUBSELECT)
    @ManyToMany(targetEntity = HotelFacilities.class,cascade = CascadeType.ALL, fetch = FetchType.EAGER)
    @JoinTable(name = "hms_room_hotel_facilities",
            //当前对象在中间表中的外键
            joinColumns = {@JoinColumn(name = "room_id", referencedColumnName = "id")},
            //对方对象在中间表中的外键
            inverseJoinColumns = {@JoinColumn(name = "hotel_facilities_id", referencedColumnName = "id")}
    )
    private List<HotelFacilities> hotelFacilitiesList;

    /**
     * 酒店房源
     */
    @ManyToMany(targetEntity = AreaAddress.class, fetch = FetchType.EAGER)
    @JoinTable(name = "hms_area_address_rooms",
            //当前对象在中间表中的外键
            joinColumns = {@JoinColumn(name = "area_address_id", referencedColumnName = "id")},
            //对方对象在中间表中的外键
            inverseJoinColumns = {@JoinColumn(name = "room_id", referencedColumnName = "id")}
    )
    public List<AreaAddress> areaAddressList;
}
